package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JSONReturn jsonReturn;

    //查询所有未退住的客户（状态为0或2）
    @RequestMapping("get_all_customers")
    public String getAll(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            // 添加过滤条件，只查询未退住的客户（状态为0或2）
            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Customer::getStatus, 0, 2);
            List<Customer> list = customerService.list(queryWrapper);
            PageInfo<Customer> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    //对未退住的客户，通过姓名进行模糊查询
    @RequestMapping("get_customers_by")
    public String getByCondition(Customer customer, PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());

            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            // 添加过滤条件，只查询未退住的客户
            queryWrapper.in(Customer::getStatus, 0);
            queryWrapper.eq(!ObjectUtils.isEmpty(customer.getId()), Customer::getId, customer.getId())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerAge()), Customer::getCustomerAge, customer.getCustomerAge())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerSex()), Customer::getCustomerSex, customer.getCustomerSex())
                    .like(!ObjectUtils.isEmpty(customer.getCustomerName()), Customer::getCustomerName, customer.getCustomerName())
                    .eq(!ObjectUtils.isEmpty(customer.getBuildingNo()), Customer::getBuildingNo, customer.getBuildingNo());
            List<Customer> customers = customerService.list(queryWrapper);
            PageInfo<Customer> pageInfo1 = new PageInfo<>(customers);
            return jsonReturn.returnSuccess(pageInfo1);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_customer")
    public String addOne(@RequestBody Customer customer) {
        try {
            customerService.save(customer);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getcustomerby_id")
    public String getById(@RequestParam("customerid") Integer customerid) {
        try {
            // 添加过滤条件，只查询未退住的客户
            //查询stutus为0
            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Customer::getStatus, 0);
            queryWrapper.eq(Customer::getId, customerid);
            Customer customer = customerService.getOne(queryWrapper);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    @RequestMapping("getcustomerby_iid")
    public String getByIid(@RequestParam("customerid") Integer customerid) {
        try {
            // 添加过滤条件，只查询未退住的客户
            //查询stutus为0或2的客户，为0 2是未退住
            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Customer::getStatus, 0,1);
            queryWrapper.eq(Customer::getId, customerid);
            Customer customer = customerService.getOne(queryWrapper);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    //修改客户信息
    @RequestMapping("modifycustomerby_id")
    public String modifyById(@RequestBody Customer customer) {
        try {
            customerService.updateById(customer);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removecustomer_by_id")
    public String removeByid(@RequestParam("customerid") Integer customerid) {
        try {
            customerService.removeById(customerid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

        // 根据分类获取客户信息，支持分页
        //查询getIsRetreated为0 2的客户，为0 2是未退住
        //查询getIsRetreated为1的客户，为1是已退住
        @PostMapping("/get_customers_by_category")
        public String getCustomersByCategory(@RequestBody Map<String, Object> params) {
            try {
                int pageNum = (Integer) params.get("pageNum");
                int pageSize = (Integer) params.get("pageSize");
                String category = (String) params.get("category");
                PageHelper.startPage(pageNum, pageSize);
                LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
                if ("active".equals(category)) {
                    queryWrapper.in(Customer::getStatus, 0);// 未退住
                } else if ("inactive".equals(category)) {
                    queryWrapper.eq(Customer::getStatus, 1); // 已退住
                }
                List<Customer> list = customerService.list(queryWrapper);
                PageInfo<Customer> pageInfo = new PageInfo<>(list);
                return jsonReturn.returnSuccess(pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
                return jsonReturn.returnError(e.getMessage());
            }
        }

        // 根据客户姓名模糊查询客户信息
        @PostMapping("/get_customers_by")
        public String getByCondition(@RequestBody Map<String, Object> params) {
            try {
                int pageNum = (Integer) params.get("pageNum");
                int pageSize = (Integer) params.get("pageSize");
                String customerName = (String) params.get("customerName");
                PageHelper.startPage(pageNum, pageSize);
                LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.like(!ObjectUtils.isEmpty(customerName), Customer::getCustomerName, customerName);
                List<Customer> customers = customerService.list(queryWrapper);
                PageInfo<Customer> pageInfo = new PageInfo<>(customers);
                return jsonReturn.returnSuccess(pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
                return jsonReturn.returnError(e.getMessage());
            }
        }

    }

